#include "user_main.h"



void user_main()
{
    vector user_data = NULL;
    user_data = user_Init(); // 启动

    user_Function(user_data);

    // main_user(user_data); // 被调用
}

vector user_Init()
{
    user_Vector_Find_Init();

    vector vc = user_read_file_data();

    return vc;
}

void user_Function(vector vc)
{
    int i = 0;
    void (*func_all[4])(vector v) = {user_Vector_Add, user_Vector_Del, user_Vector_Upd, user_Vector_Find};

    char Function_ptf[][10] = {"增", "删", "改", "查"}, get_ch, c;

    while (1)
    {
        printf("----------\n");
        for (i = 0; i < sizeof(Function_ptf) / sizeof(Function_ptf[0]); i++)
        {
            printf("%d.%s\n", i + 1, Function_ptf[i]);
        }
        printf("0.退出\n");
        printf("\n:>");

        get_ch = getchar();
        while ('\n' != get_ch && '\n' != (c = getchar())) // 一个回车也会进入下一循环
            ;

        get_ch -= '0';

        if (0 == get_ch)
            return;

        if (1 > get_ch || i < get_ch)
            printf("input error \n");

        func_all[get_ch - 1](vc);//
    }
}

// 匹配ID排序的链表排序的cmp函数
int user_sort_link_cmp(const void *dast, const void *str)
{
    struct _role_inf *p = (struct _role_inf *)dast;
    struct _role_inf *q = (struct _role_inf *)str;

    if (strcmp(p->ID, q->ID) >= 0)
        return 1;

    return -1;
}

// 读取数据文件放入顺序表
vector user_read_file_data(void)
{
    list_inf l = NULL;
    vector vc = (vector)malloc(sizeof(struct _vector));
    size_t i = 0;
    l = link_create(); // 创建链表

    FILE *file = fopen(USER_DATA_FILE, "r+b");

    data_type *temp = (data_type *)calloc(1, sizeof(data_type)); // 链表数据域

    while (fread(temp, sizeof(data_type), 1, file)) // 读文件存入链表
    {

        if (0 == strlen(temp->_USER.ID))
        {
            free(temp);
            temp = (data_type *)calloc(1, sizeof(data_type));
            continue;
        }

        printf("\n");

        link_push_back(l, temp); // 尾插，链表加节点，节点存入数据

        temp = (data_type *)calloc(1, sizeof(data_type)); // 再剔除
    }

    if (-1 == 0)
    {
        fprintf(stderr, "read_file_data::读取出错!\n");
        exit(1);
    }

    free(temp);

    link_sort(l, user_sort_link_cmp); // 链表排序
    vc = link_to_vector(l);           // 传入
    link_destory(l);                  // 销毁

    fclose(file);

    return vc;
}

// 直接读数据进入vector
void user_fwrite_vector(vector v)
{
    FILE *fp = fopen("inf.bin", "w");
    fread(v->buff, sizeof(v->buff) * v->size, 1, fp);
    fclose(fp);
}

// 将链表数据导入顺序表，链表不销毁
vector link_to_vector(list_inf l)
{
    int i = 0;
    vector v = vector_create(l->size + l->size / 5);
    node *p = l->head->next;

    while (NULL != p)
    {
        v->buff[i] = *p->data;
        v->size++;
        p = p->next;
        user_show_inf(&(v->buff[i]));
        printf("\n");
        i++;
    }

    return v;
}

// 顺序表数据导入链表,源顺序表不销毁
list_inf user_vector_to_link(vector v)
{
    int i = 0;
    list_inf l = link_create();
    node *p = l->head;

    while (i < v->size)
    {
        elem_type *temp = (elem_type *)malloc(sizeof(elem_type)); // 创建新的空间给链表
        *temp = v->buff[i++];
        link_push_back(l, temp);
    }

    return l;
}